package com.yt.phone.service;



import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.baomidou.mybatisplus.mapper.Wrapper;

import com.baomidou.mybatisplus.plugins.Page;

import com.yt.phone.mapper.PhoneBillMapper;

import com.yt.phone.models.dto.BillDTO;

import com.yt.phone.models.dto.PhoneBillDTO;

import com.yt.phone.models.vo.BillVO;
import org.apache.dubbo.config.annotation.DubboService;

import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

import com.yt.phone.common.config.YmlConfig;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.yt.phone.mapper.BillMapper;

import com.yt.phone.models.service.IBillService;

import com.yt.phone.models.bo.BillSaveBO;


import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;



/**

 * 账单信息控制器

 *

 * @author lizicong

 * @Date 2025-11-05 23:40:06

 */

@DubboService

public class BillServiceImpl extends ServiceImpl<BillMapper, BillDTO> implements IBillService{



    @Resource

    private YmlConfig ymlConfig;

    @Resource

    private BillMapper billMapper;

    @Resource

    private PhoneBillMapper phoneBillMapper;



    @Override

    public boolean add(BillSaveBO saveBO) {

        BillDTO dto = new BillDTO();

        BeanUtils.copyProperties(saveBO,dto);

        Long id = ymlConfig.getPkId();

        dto.setId(id);

        return this.insert(dto);

    }

    @Override

    public List<BillDTO> getbillList() {

        List<BillDTO> billList = billMapper.selectList(null);

        return billList;

    }



    @Override

    public Page<BillVO> getpagebillList(int currentPage,String keyword) {
        if (currentPage < 1) {
            currentPage = 1;
        }

        if (keyword == null) {
            keyword = "";
        }

        int pageSize = 5;
        int offset = (currentPage - 1) * pageSize;
        List<BillVO> records = billMapper.queryBill(keyword,offset, pageSize);
        int totalCount = billMapper.countBillWithUserName(keyword);

        Page<BillVO> page = new Page<>(currentPage, pageSize);
        page.setRecords(records);
        page.setTotal(totalCount);
        return page;
    }



    @Override

    public List<BillDTO> searchgetbillList(String keyword) {

        Wrapper<BillDTO> wrapper = new EntityWrapper<>();

        wrapper.like("bill", keyword)

                .or()

                .like("bill_id", keyword);

        List<BillDTO> billList = billMapper.selectList(wrapper);

        return billList;

    }

    @Override

    public List<BillDTO> getBillListByUserId(Long userId) {

        Wrapper<PhoneBillDTO> wrapper = new EntityWrapper<>();

        wrapper.eq("user_id", userId);

        List<PhoneBillDTO> billList = phoneBillMapper.selectList(wrapper);

        List<BillDTO> resultList =new ArrayList<>();

        if (billList != null && !billList.isEmpty()) {

            List<Long> billIds = billList.stream()

                    .map(PhoneBillDTO::getBillId)

                    .collect(Collectors.toList());

            Wrapper<BillDTO> billWrapper = new EntityWrapper<>();

            billWrapper.in("bill_id", billIds);

            resultList = this.selectList(billWrapper);

        }

        return resultList;

    }

    

    /**

     * 根据支付状态查询账单列表

     * @param payStatus 支付状态

     * @return 账单列表

     */

    public List<BillDTO> getBillListByPayStatus(Integer payStatus) {

        Wrapper<BillDTO> wrapper = new EntityWrapper<>();

        wrapper.eq("pay_status", payStatus);

        return billMapper.selectList(wrapper);

    }
    
    /**
     * 根据账单状态查询账单列表
     * @param status 账单状态 0-未计费 1-已计费
     * @return 账单列表
     */
    public List<BillDTO> getBillListByStatus(Integer status) {
        Wrapper<BillDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("status", status);
        return billMapper.selectList(wrapper);
    }
    
    /**

     * 更新账单支付信息

     * @param billId 账单ID

     * @param payAmount 实际支付金额

     * @param payMethod 支付方式

     * @param payStatus 支付状态

     * @return 是否更新成功

     */
    public boolean updatePayInfo(Long billId, Double payAmount, String payMethod, Integer payStatus) {
        BillDTO bill = new BillDTO();
        bill.setBillId(billId);
        bill.setPayAmount(payAmount);
        bill.setPayStatus(payStatus);
        // 同时更新账单的计费状态，1表示已计费
        bill.setStatus(1);

        Wrapper<BillDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("bill_id", billId);
        
        return billMapper.update(bill, wrapper) > 0;
    }

}
